<template>
  <div>
    <t-form labelWidth="120" ref="form">
      <div class="top flex-c jc-sb">
        <div class="left flex-c">
          <img src="@/assets/image/addAccount.png" alt="" srcset="">
          <span>默认情况下密码为新增账号名称大写首字母加手机号后六位</span>
        </div>
      </div>

      <div class="pageBg mtb-16 content">
        <t-form-item label="新增账号手机号" name="phone">
          <t-input v-model="formData.phone" placeholder="请输入手机号" type="number" disabled></t-input>
        </t-form-item>
        <t-form-item label="新增账号名称" name="name">
          <t-input v-model="formData.name" placeholder="请输入账号名称" :maxlength="10" disabled></t-input>
        </t-form-item>
        <t-form-item label="新增账号角色（可多选）" name="roles">
          <div class="flex-c addRoles">
            <div class="rolesItem" v-for="(item, index) in formData.roles" :key="index">
              <span v-if="item == '1'">老板</span>
              <span v-if="item == '2'">店长</span>
              <span v-if="item == '3'">前台</span>
              <span v-if="item == '4'">其他角色</span>
            </div>
          </div>
        </t-form-item>
        <t-form-item label="授权店铺" name="shop">
          <t-select v-model="formData.shop" disabled>
            <t-option key="apple" label="Apple" value="apple" />
            <t-option key="orange" value="orange">Orange</t-option>
            <t-option key="banana" label="Banana" value="banana" />
          </t-select>
        </t-form-item>
      </div>

      <div class="pageBg">
        <div class="pageTitle mb-16">账号权限分配</div>
        <div class="jurisdiction">
          <div class="item" v-for="(item, index) in formData.jurisdiction" :key="index">
            {{ getJurisdiction(item) }}
          </div>
        </div>
      </div>
    </t-form>
  </div>
</template>

<script>
export default {
  name: '',
  data() {
    return {
      formData: {
        phone: "",
        name: "",
        roles: ["1", "4"],
        otherRole: "",
        // shop: "",
        jurisdiction: ["1", "4"],
      },
      type: 'add',
      jurisdictionList: [
        {
          label: "工作中心",
          value: "1"
        },
        {
          label: "任务中心",
          value: "2"
        },
        {
          label: "数据中心",
          value: "3"
        },
        {
          label: "经营指南",
          value: "4"
        },
        {
          label: "信息管理",
          value: "5"
        },
        {
          label: "财务管理",
          value: "6"
        },
        {
          label: "评价管理",
          value: "7"
        },
        {
          label: "订单管理",
          value: "8"
        },
        {
          label: "房态房价",
          value: "9"
        },
        {
          label: "系统管理",
          value: "10"
        },
      ],
      rules: {
        phone: [{ required: true, message: '请填写新增账号手机号', type: 'error', trigger: 'blur' }],
        name: [{ required: true, message: '请填写新增账号名称', type: 'error', trigger: 'blur' }],
        roles: [
          { required: true, message: '请选择新增账号角色', type: 'error', trigger: 'blur' },
        ],
        shop: [{ required: true, message: '请选择授权店铺', type: 'error', trigger: 'blur' }],
        otherRole: [{ required: true, message: '请选择新增账号角色', type: 'error', trigger: 'blur' }, { validator: this.rolesValidator }],
      },
      isOtherRples: false
    }
  },
  created() {
    this.type = this.$route.query.type
  },
  methods: {
    onSubmit({ validateResult, firstError }) {
      if (validateResult === true) {
        this.$message.success('提交成功');
      } else {
        console.log('Errors: ', validateResult);
        this.$message.warning(firstError);
      }
    },
    onRolesChange(checked, e) {
      if (e.current === '4') {
        if (e.type == 'uncheck') {
          this.isOtherRples = false
        } else {
          this.isOtherRples = true
        }
      }
    },
    rolesValidator(val) {
      if (this.formData.roles.includes("4") && !val) {
        return { result: true, message: '请输入角色', type: 'error' };
      }
      return { result: true, type: 'error' }
    },
    getJurisdiction(val) {
      return this.jurisdictionList.find(item => item.value == val).label
    }
  },
}
</script>
<style lang='less' scoped>
.left {
  padding: 5px 12px;
  background-color: #FFE4BA;
  font-size: 14px;
  color: #FF9A2E;

  img {
    width: 14px;
    height: 14px;
  }

  span {
    margin-left: 12px;
  }
}

.mtb-16 {
  margin: 16px 0;
}

.mb-16 {
  margin-bottom: 16px;
}

.content {
  display: flex;

  ::v-deep .t-form__item {
    margin-right: 24px;
  }

  ::v-deep .t-input__wrap {
    width: 220px;
  }

  .addRoles {
    ::v-deep .t-checkbox-group {

      .t-checkbox {
        width: 80px;
        margin-right: 16px;
      }
    }

  }
}

.rolesItem {
  width: 120px;
  font-size: 14px;
  color: #1D2129;
  padding: 7px 12px;
  margin-right: 16px;
  background-color: #F2F3F5;
}

.jurisdiction {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;

  .item {
    padding: 9px 23px;
    box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.05);
    border-radius: 2px;
    border: 1px solid #E5E6EB;
  }
}

::v-deep .t-form__controls.t-is-success .t-input__extra {
  color: #d54941;
}

::v-deep .t-checkbox-group {
  width: 100%;
  justify-content: space-between;
}

::v-deep .t-input.t-is-disabled {
  background-color: #F2F3F5;
}
</style>